Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./year2.RDS")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.25, n = 477)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 11.37841 11.43384 11.48830 11.54181 11.59438 11.64600 11.69670 11.74647
##   [9] 11.79534 11.84330 11.89037 11.93655 11.98186 12.02631 12.06989 12.11263
##  [17] 12.15453 12.19559 12.23584 12.27527 12.31391 12.35174 12.38874 12.42489
##  [25] 12.46016 12.49452 12.52795 12.56042 12.59192 12.62240 12.65185 12.68024
##  [33] 12.70755 12.73375 12.75917 12.78407 12.80840 12.83205 12.85494 12.87700
##  [41] 12.89814 12.91828 12.93733 12.95521 12.97184 12.98714 13.00101 13.01339
##  [49] 13.02470 13.03541 13.04542 13.05466 13.06305 13.07051 13.07695 13.08231
##  [57] 13.08649 13.08941 13.09101 13.09119 13.08988 13.08699 13.08112 13.07125
##  [65] 13.05793 13.04168 13.02304 13.00255 12.98074 12.95815 12.93530 12.91274
##  [73] 12.89100 12.87062 12.85212 12.83605 12.81905 12.79788 12.77320 12.74568
##  [81] 12.71598 12.68478 12.65274 12.62053 12.58882 12.55827 12.52956 12.50335
##  [89] 12.48031 12.46111 12.44310 12.42343 12.40245 12.38052 12.35801 12.33527
##  [97] 12.31266 12.29054 12.26927 12.24920 12.23070 12.21412 12.19983 12.18818
## [105] 12.17897 12.17162 12.16593 12.16169 12.15868 12.15670 12.15555 12.15501
## [113] 12.15487 12.15494 12.15499 12.15483 12.15424 12.15301 12.15209 12.15245
## [121] 12.15395 12.15645 12.15979 12.16383 12.16843 12.17344 12.17872 12.18411
## [129] 12.18948 12.19469 12.19957 12.20400 12.20782 12.21089 12.21327 12.21520
## [137] 12.21677 12.21811 12.21932 12.22050 12.22176 12.22322 12.22497 12.22713
## [145] 12.22980 12.23309 12.23711 12.24197 12.24777 12.25462 12.26263 12.27191
## [153] 12.28256 12.29564 12.31181 12.33055 12.35139 12.37383 12.39736 12.42149
## [161] 12.44574 12.46959 12.49256 12.51414 12.53385 12.55119 12.56566 12.58015
## [169] 12.59764 12.61773 12.64006 12.66425 12.68991 12.71668 12.74416 12.77199
## [177] 12.79979 12.82718 12.85378 12.87921 12.90309 12.92505 12.94471 12.96169
## [185] 12.97561 12.98610 12.99277 12.99526 12.99524 12.99461 12.99330 12.99121
## [193] 12.98826 12.98438 12.97949 12.97349 12.96631 12.95788 12.94809 12.93689
## [201] 12.92417 12.90987 12.89145 12.86698 12.83729 12.80319 12.76553 12.72512
## [209] 12.68280 12.63938 12.59570 12.55258 12.51084 12.47132 12.43483 12.40221
## [217] 12.36861 12.32929 12.28524 12.23744 12.18689 12.13458 12.08150 12.02864
## [225] 11.97699 11.92753 11.88127 11.83918 11.80226 11.77150 11.74335 11.71387
## [233] 11.68348 11.65259 11.62160 11.59092 11.56097 11.53215 11.50487 11.47954
## [241] 11.45657 11.43636 11.41934 11.40590 11.39592 11.38880 11.38432 11.38221
## [249] 11.38223 11.38413 11.38767 11.39259 11.39865 11.40559 11.41318 11.42116
## [257] 11.42928 11.43730 11.44496 11.45203 11.46108 11.47444 11.49149 11.51159
## [265] 11.53411 11.55841 11.58385 11.60982 11.63567 11.66078 11.68450 11.70620
## [273] 11.72526 11.74104 11.75582 11.77213 11.78976 11.80846 11.82802 11.84820
## [281] 11.86877 11.88950 11.91016 11.93053 11.95037 11.96945 11.98755 12.00444
## [289] 12.02054 12.03647 12.05223 12.06783 12.08327 12.09857 12.11374 12.12878
## [297] 12.14370 12.15851 12.17322 12.18784 12.20237 12.21683 12.23107 12.24496
## [305] 12.25853 12.27180 12.28480 12.29755 12.31008 12.32242 12.33460 12.34664
## [313] 12.35857 12.37041 12.38219 12.39394 12.40569 12.41745 12.42926 12.44115
## [321] 12.45313 12.46584 12.47973 12.49461 12.51027 12.52650 12.54310 12.55988
## [329] 12.57662 12.59312 12.60918 12.62460 12.63916 12.65268 12.66493 12.67573
## [337] 12.68533 12.69415 12.70228 12.70979 12.71676 12.72326 12.72937 12.73515
## [345] 12.74070 12.74607 12.75135 12.75661 12.76192 12.76704 12.77168 12.77587
## [353] 12.77964 12.78301 12.78603 12.78871 12.79108 12.79319 12.79504 12.79668
## [361] 12.79812 12.79941 12.80057 12.80117 12.80085 12.79975 12.79798 12.79568
## [369] 12.79297 12.78998 12.78684 12.78367 12.78059 12.77775 12.77526 12.77325
## [377] 12.77185 12.76966 12.76545 12.75960 12.75247 12.74443 12.73586 12.72712
## [385] 12.71858 12.71061 12.70358 12.69786 12.69382 12.69184 12.69227 12.69492
## [393] 12.69915 12.70472 12.71137 12.71884 12.72688 12.73524 12.74366 12.75188
## [401] 12.75966 12.76674 12.77286 12.77778 12.78122 12.78482 12.79012 12.79682
## [409] 12.80458 12.81307 12.82198 12.83098 12.83974 12.84795 12.85526 12.86137
## [417] 12.86595 12.86866 12.86919 12.86781 12.86513 12.86134 12.85661 12.85114
## [425] 12.84512 12.83872 12.83214 12.82556 12.81917 12.81315 12.80770 12.80298
## [433] 12.79920 12.79580 12.79213 12.78819 12.78401 12.77960 12.77499 12.77018
## [441] 12.76521 12.76008 12.75481 12.74942 12.74393 12.73836 12.73272 12.72704
## [449] 12.72132 12.71547 12.70937 12.70304 12.69650 12.68978 12.68288 12.67584
## [457] 12.66866 12.66137 12.65399 12.64653 12.63902 12.63147 12.62391 12.61636
## [465] 12.60885 12.60135 12.59386 12.58636 12.57883 12.57127 12.56366 12.55599
## [473] 12.54823 12.54039 12.53243 12.52436 12.51616
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.25, n = 477)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 10.86494 10.94133 11.01643 11.09021 11.16268 11.23382 11.30362 11.37206
##   [9] 11.43915 11.50486 11.56918 11.63212 11.69365 11.75376 11.81245 11.86970
##  [17] 11.92550 11.97984 12.03272 12.08411 12.13400 12.18238 12.22930 12.27477
##  [25] 12.31884 12.36153 12.40288 12.44292 12.48169 12.51921 12.55552 12.59065
##  [33] 12.62463 12.65750 12.68885 12.71832 12.74601 12.77202 12.79647 12.81946
##  [41] 12.84108 12.86146 12.88068 12.89886 12.91610 12.93251 12.94819 12.96324
##  [49] 12.97654 12.98708 12.99514 13.00100 13.00495 13.00727 13.00825 13.00816
##  [57] 13.00730 13.00595 13.00438 13.00289 13.00176 13.00127 13.00031 12.99769
##  [65] 12.99359 12.98815 12.98155 12.97396 12.96553 12.95643 12.94683 12.93689
##  [73] 12.92678 12.91665 12.90668 12.89703 12.88729 12.87695 12.86602 12.85449
##  [81] 12.84237 12.82966 12.81637 12.80248 12.78802 12.77297 12.75734 12.74113
##  [89] 12.72435 12.70700 12.68732 12.66398 12.63757 12.60868 12.57791 12.54585
##  [97] 12.51309 12.48024 12.44789 12.41662 12.38704 12.35975 12.33532 12.31437
## [105] 12.29335 12.26878 12.24138 12.21187 12.18095 12.14933 12.11773 12.08686
## [113] 12.05743 12.03015 12.00574 11.98490 11.96834 11.95679 11.94853 11.94138
## [121] 11.93532 11.93033 11.92639 11.92347 11.92156 11.92062 11.92065 11.92162
## [129] 11.92350 11.92628 11.92993 11.93444 11.93978 11.94592 11.95413 11.96545
## [137] 11.97962 11.99634 12.01534 12.03631 12.05899 12.08309 12.10832 12.13440
## [145] 12.16104 12.18795 12.21486 12.24148 12.26752 12.29270 12.31674 12.33934
## [153] 12.36024 12.38278 12.41003 12.44118 12.47546 12.51208 12.55026 12.58920
## [161] 12.62814 12.66627 12.70281 12.73698 12.76800 12.79508 12.81743 12.83781
## [169] 12.85934 12.88183 12.90506 12.92883 12.95293 12.97714 13.00126 13.02509
## [177] 13.04841 13.07101 13.09269 13.11324 13.13245 13.15011 13.16602 13.17996
## [185] 13.19172 13.20111 13.20790 13.21189 13.21462 13.21760 13.22060 13.22337
## [193] 13.22570 13.22734 13.22806 13.22763 13.22581 13.22237 13.21708 13.20970
## [201] 13.20000 13.18775 13.17161 13.15080 13.12591 13.09754 13.06629 13.03274
## [209] 12.99749 12.96113 12.92426 12.88746 12.85135 12.81650 12.78351 12.75297
## [217] 12.72108 12.68415 12.64299 12.59841 12.55123 12.50223 12.45224 12.40206
## [225] 12.35249 12.30435 12.25843 12.21556 12.17653 12.14215 12.10840 12.07115
## [233] 12.03110 11.98894 11.94537 11.90106 11.85673 11.81304 11.77071 11.73041
## [241] 11.69283 11.65868 11.62864 11.60339 11.58111 11.55953 11.53874 11.51883
## [249] 11.49987 11.48196 11.46518 11.44961 11.43533 11.42243 11.41099 11.40109
## [257] 11.39283 11.38627 11.38152 11.37864 11.37874 11.38253 11.38957 11.39942
## [265] 11.41162 11.42572 11.44128 11.45785 11.47498 11.49222 11.50913 11.52525
## [273] 11.54013 11.55334 11.56701 11.58337 11.60204 11.62269 11.64494 11.66845
## [281] 11.69285 11.71779 11.74291 11.76785 11.79226 11.81578 11.83806 11.85873
## [289] 11.88008 11.90432 11.93098 11.95960 11.98969 12.02079 12.05243 12.08414
## [297] 12.11545 12.14589 12.17498 12.20226 12.22726 12.24951 12.27015 12.29064
## [305] 12.31097 12.33110 12.35103 12.37074 12.39020 12.40940 12.42831 12.44693
## [313] 12.46523 12.48319 12.50079 12.51802 12.53486 12.55128 12.56728 12.58282
## [321] 12.59789 12.61249 12.62663 12.64035 12.65367 12.66663 12.67926 12.69159
## [329] 12.70364 12.71546 12.72708 12.73851 12.74980 12.76098 12.77207 12.78310
## [337] 12.79357 12.80301 12.81156 12.81938 12.82658 12.83332 12.83973 12.84596
## [345] 12.85213 12.85839 12.86488 12.87174 12.87910 12.88667 12.89406 12.90129
## [353] 12.90835 12.91526 12.92202 12.92863 12.93511 12.94145 12.94767 12.95377
## [361] 12.95975 12.96562 12.97140 12.97699 12.98236 12.98751 12.99248 12.99729
## [369] 13.00197 13.00653 13.01100 13.01541 13.01979 13.02415 13.02852 13.03293
## [377] 13.03740 13.04185 13.04618 13.05040 13.05451 13.05853 13.06246 13.06630
## [385] 13.07007 13.07376 13.07740 13.08098 13.08451 13.08800 13.09145 13.09541
## [393] 13.10025 13.10580 13.11186 13.11824 13.12473 13.13116 13.13733 13.14303
## [401] 13.14809 13.15230 13.15548 13.15742 13.15795 13.15789 13.15816 13.15863
## [409] 13.15916 13.15964 13.15994 13.15994 13.15952 13.15854 13.15689 13.15443
## [417] 13.15105 13.14662 13.14102 13.13347 13.12353 13.11158 13.09801 13.08319
## [425] 13.06752 13.05138 13.03514 13.01919 13.00391 12.98969 12.97690 12.96593
## [433] 12.95717 12.94936 12.94106 12.93235 12.92330 12.91400 12.90452 12.89494
## [441] 12.88533 12.87579 12.86638 12.85719 12.84829 12.83976 12.83169 12.82414
## [449] 12.81720 12.81053 12.80378 12.79697 12.79014 12.78333 12.77657 12.76990
## [457] 12.76336 12.75697 12.75079 12.74483 12.73914 12.73376 12.72871 12.72405
## [465] 12.71978 12.71588 12.71231 12.70906 12.70610 12.70341 12.70095 12.69872
## [473] 12.69668 12.69480 12.69308 12.69147 12.68995
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.25, n = 477)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 10.86902 10.92782 10.98565 11.04249 11.09835 11.15323 11.20711 11.26001
##   [9] 11.31191 11.36282 11.41273 11.46164 11.50955 11.55645 11.60235 11.64724
##  [17] 11.69112 11.73398 11.77584 11.81667 11.85650 11.89533 11.93316 11.97001
##  [25] 12.00585 12.04071 12.07457 12.10743 12.13930 12.17017 12.20005 12.22894
##  [33] 12.25683 12.28373 12.30947 12.33393 12.35716 12.37921 12.40013 12.41997
##  [41] 12.43879 12.45663 12.47355 12.48960 12.50483 12.51929 12.53304 12.54611
##  [49] 12.55843 12.56985 12.58040 12.59006 12.59884 12.60675 12.61378 12.61994
##  [57] 12.62524 12.62967 12.63324 12.63594 12.63780 12.63879 12.63809 12.63501
##  [65] 12.62985 12.62287 12.61436 12.60461 12.59388 12.58246 12.57062 12.55866
##  [73] 12.54684 12.53545 12.52477 12.51507 12.50424 12.49029 12.47370 12.45496
##  [81] 12.43455 12.41295 12.39066 12.36814 12.34588 12.32437 12.30409 12.28552
##  [89] 12.26914 12.25545 12.24263 12.22869 12.21384 12.19828 12.18222 12.16587
##  [97] 12.14944 12.13312 12.11713 12.10168 12.08696 12.07319 12.06057 12.04930
## [105] 12.03913 12.02959 12.02065 12.01223 12.00428 11.99674 11.98956 11.98269
## [113] 11.97605 11.96960 11.96329 11.95704 11.95081 11.94454 11.93819 11.93179
## [121] 11.92539 11.91904 11.91278 11.90665 11.90071 11.89500 11.88957 11.88446
## [129] 11.87971 11.87538 11.87151 11.86815 11.86534 11.86312 11.86075 11.85755
## [137] 11.85370 11.84936 11.84471 11.83991 11.83513 11.83056 11.82635 11.82268
## [145] 11.81972 11.81764 11.81661 11.81681 11.81839 11.82154 11.82642 11.83320
## [153] 11.84206 11.85329 11.86687 11.88248 11.89981 11.91856 11.93842 11.95907
## [161] 11.98020 12.00151 12.02268 12.04341 12.06338 12.08229 12.09981 12.11895
## [169] 12.14256 12.17014 12.20117 12.23516 12.27160 12.30999 12.34983 12.39060
## [177] 12.43182 12.47296 12.51353 12.55302 12.59094 12.62677 12.66001 12.69016
## [185] 12.71671 12.73916 12.75700 12.76974 12.78016 12.79118 12.80253 12.81389
## [193] 12.82498 12.83550 12.84516 12.85366 12.86070 12.86600 12.86925 12.87017
## [201] 12.86846 12.86381 12.85431 12.83876 12.81797 12.79276 12.76395 12.73235
## [209] 12.69878 12.66404 12.62897 12.59437 12.56106 12.52985 12.50157 12.47701
## [217] 12.45159 12.42073 12.38535 12.34638 12.30471 12.26125 12.21694 12.17266
## [225] 12.12934 12.08788 12.04921 12.01422 11.98384 11.95897 11.93689 11.91437
## [233] 11.89160 11.86875 11.84597 11.82346 11.80138 11.77990 11.75920 11.73945
## [241] 11.72081 11.70348 11.68761 11.67337 11.66081 11.64977 11.64017 11.63189
## [249] 11.62484 11.61893 11.61406 11.61014 11.60706 11.60473 11.60305 11.60192
## [257] 11.60126 11.60095 11.60091 11.60104 11.60234 11.60572 11.61093 11.61772
## [265] 11.62583 11.63503 11.64505 11.65566 11.66659 11.67760 11.68845 11.69887
## [273] 11.70862 11.71745 11.72621 11.73583 11.74621 11.75721 11.76872 11.78062
## [281] 11.79280 11.80513 11.81750 11.82978 11.84186 11.85363 11.86495 11.87571
## [289] 11.88673 11.89877 11.91169 11.92536 11.93961 11.95431 11.96930 11.98444
## [297] 11.99958 12.01457 12.02927 12.04353 12.05720 12.07014 12.08335 12.09782
## [305] 12.11339 12.12988 12.14711 12.16491 12.18310 12.20152 12.21999 12.23832
## [313] 12.25636 12.27392 12.29084 12.30693 12.32202 12.33594 12.34852 12.35958
## [321] 12.36894 12.37677 12.38340 12.38897 12.39361 12.39744 12.40059 12.40319
## [329] 12.40537 12.40725 12.40896 12.41063 12.41238 12.41435 12.41666 12.41943
## [337] 12.42222 12.42452 12.42639 12.42790 12.42911 12.43009 12.43090 12.43161
## [345] 12.43227 12.43295 12.43372 12.43464 12.43577 12.43637 12.43580 12.43423
## [353] 12.43185 12.42883 12.42536 12.42163 12.41781 12.41408 12.41063 12.40765
## [361] 12.40530 12.40378 12.40327 12.40277 12.40131 12.39906 12.39623 12.39299
## [369] 12.38953 12.38604 12.38270 12.37971 12.37725 12.37551 12.37468 12.37493
## [377] 12.37647 12.37929 12.38317 12.38796 12.39352 12.39971 12.40639 12.41341
## [385] 12.42063 12.42790 12.43509 12.44204 12.44862 12.45468 12.46008 12.46604
## [393] 12.47368 12.48270 12.49278 12.50362 12.51492 12.52637 12.53767 12.54850
## [401] 12.55857 12.56756 12.57518 12.58110 12.58504 12.58806 12.59137 12.59486
## [409] 12.59844 12.60199 12.60542 12.60861 12.61148 12.61390 12.61578 12.61701
## [417] 12.61750 12.61713 12.61580 12.61309 12.60880 12.60316 12.59643 12.58882
## [425] 12.58058 12.57195 12.56316 12.55444 12.54604 12.53819 12.53113 12.52510
## [433] 12.52032 12.51604 12.51137 12.50634 12.50101 12.49541 12.48959 12.48360
## [441] 12.47747 12.47126 12.46501 12.45876 12.45255 12.44643 12.44045 12.43465
## [449] 12.42907 12.42352 12.41778 12.41188 12.40584 12.39969 12.39345 12.38714
## [457] 12.38079 12.37443 12.36807 12.36174 12.35546 12.34926 12.34317 12.33720
## [465] 12.33135 12.32561 12.31996 12.31439 12.30888 12.30342 12.29798 12.29257
## [473] 12.28715 12.28171 12.27625 12.27073 12.26516
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")

keeping in case

#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")